2024-09-22 jsdom - Javascript testing
I have been using jsdom package to test an application that I am writing which is browser based.
The app is a simple Markdown editor with the screen split in two one side Markdown and the other rendered code. There is a toolbar with various Markdown shortcuts to make it easier.
I am in fact writing this post using the editor.
I wanted to write some tests particularly on the text manipulation and that is what I have used jsdom to do.
Generally it was an easy process, the docs are OK but I could have done with a few more good examples but I managed to put together a working test very quickly.
The only problem I had was loading one particular script marked which I could not access from my tests, all of the other scripts in the same folder were fine, just this one. It also worked fine in the chrome browser.
I searched the docs and tried various options all to no avail but in the end I created a workaround in that I loaded the module in my test script and in the options for jsdom I added the following:
beforeParse: function (window) {
window.marked = marked
}
Not sure if that was a valid solution but my scripts then worked fine.
jsdom is a powerful package and I will continue to use it for this testing and no doubt as I get more familiar I will realise what I may have done wrong in using marked but for now all is fine.